package com.axin.oss.controller;

import com.axin.common.core.annotation.RepeatSubmit;
import com.axin.common.core.domain.web.AjaxResult;
import com.axin.common.core.domain.web.TableDataInfo;
import com.axin.common.core.validate.AddGroup;
import com.axin.common.core.validate.EditGroup;
import com.axin.common.core.validate.QueryGroup;
import com.axin.common.security.base.BaseController;
import com.axin.oss.api.dto.SysOssConfigDTO;
import com.axin.oss.api.vo.SysOssConfigVo;
import com.axin.oss.service.ISysOssConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;

/**
 * OSS配置 控制接口层
 *
 * @Author axin
 * @Date 2021/11/4 10:58
 **/
@RestController
@RequestMapping("/config")
public class SysOssConfigController extends BaseController {

    @Autowired
    private ISysOssConfigService ossConfigService;


    /**
     * 查询对象存储配置列表
     */
    @PreAuthorize("@ss.hasPermi('system:oss:list')")
    @GetMapping("/list")
    public TableDataInfo<SysOssConfigVo> list(@Validated(QueryGroup.class)SysOssConfigDTO dto) {
        return ossConfigService.queryPageList(dto);
    }


    /**
     * 新增对象存储配置
     */
    @PreAuthorize("@ss.hasPermi('system:oss:add')")
    @RepeatSubmit()
    @PostMapping()
    public AjaxResult<Void> add(@Validated(AddGroup.class) @RequestBody SysOssConfigDTO dto) {
        return toAjax(ossConfigService.insertByDTO(dto) ? 1 : 0);
    }


    /**
     * 获取对象存储配置详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:oss:query')")
    @GetMapping("/{ossConfigId}")
    public AjaxResult<SysOssConfigVo> getInfo(@NotNull(message = "主键不能为空")
                                              @PathVariable("ossConfigId") Long ossConfigId) {
        return AjaxResult.success(ossConfigService.queryById(ossConfigId));
    }

    /**
     * 修改对象存储配置
     */
    @PreAuthorize("@ss.hasPermi('system:oss:edit')")
    @RepeatSubmit()
    @PutMapping()
    public AjaxResult<Void> edit(@Validated(EditGroup.class) @RequestBody SysOssConfigDTO dto) {
        return toAjax(ossConfigService.updateByDTO(dto) ? 1 : 0);
    }


    /**
     * 删除对象存储配置
     */
    @PreAuthorize("@ss.hasPermi('system:oss:remove')")
    @DeleteMapping("/{ossConfigIds}")
    public AjaxResult<Void> remove(@NotEmpty(message = "主键不能为空")
                                   @PathVariable Long[] ossConfigIds) {
        return toAjax(ossConfigService.deleteWithValidByIds(Arrays.asList(ossConfigIds), true) ? 1 : 0);
    }

    /**
     * 状态修改
     */
    @PreAuthorize("@ss.hasPermi('system:oss:edit')")
    @PutMapping("/changeStatus")
    public AjaxResult changeStatus(@RequestBody SysOssConfigDTO dto) {
        return toAjax(ossConfigService.updateOssConfigStatus(dto));
    }



}
